<template>
  <div>
    <el-dialog :visible="showDialog" v-bind="$attrs" title="新增员工" v-on="$listeners" @open="onOpen" @close="onClose">
      <el-form ref="addEmployeeRef" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="formData.username" placeholder="请输入姓名" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="formData.mobile" placeholder="请输入手机号" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="formData.timeOfEntry"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{width: '100%'}"
            placeholder="请选择入职时间"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="formData.formOfEmployment" placeholder="请选择聘用形式" clearable :style="{width: '100%'}">
            <el-option
              v-for="(item, index) in hireType"
              :key="index"
              :label="item.value"
              :value="item.id"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="formData.workNumber" placeholder="请输入工号" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input v-model="formData.departmentName" placeholder="请输入部门" clearable :style="{width: '100%'}" @focus="getDepartments">
          </el-input>
          <el-tree v-if="showTree" v-loading="loading" :data="departs" default-expand-all :props="{label:'name'}" @node-click="onNodeClick">
          </el-tree>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="formData.correctionTime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{width: '100%'}"
            placeholder="请选择转正时间"
            clearable
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { tranListToTreeData } from '@/utils'
import { getDepartments } from '@/api/department'
import EmployeeEnum from '@/api/constant/employees.js'
import { addEmployee } from '@/api/employees'
export default {
  components: {},
  inheritAttrs: false,
  props: {
    showDialog: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      hireType: EmployeeEnum.hireType,
      departs: [],
      showTree: false,
      loading: false,
      formData: {
        username: undefined,
        mobile: undefined,
        formOfEmployment: null,
        workNumber: undefined,
        departmentName: undefined,
        timeOfEntry: '',
        correctionTime: null
      },
      rules: {
        username: [{
          required: true,
          message: '请输入单行姓名',
          trigger: 'blur'
        }, {
          min: 1,
          max: 4,
          message: '用户姓名为1-4位',
          trigger: 'blur'
        }],
        mobile: [{
          required: true,
          message: '请输入正确手机号',
          trigger: 'blur'
        }, {
          pattern: /^1[3-9]\d{9}$/,
          message: '手机号格式不正确',
          trigger: 'blur'
        }],
        formOfEmployment: [{
          required: true,
          message: '请选择入职时间',
          trigger: 'change'
        }],
        workNumber: [{
          required: true,
          message: '请选择聘用形式',
          trigger: 'change'
        }],
        departmentName: [{
          required: true,
          message: '请输入工号',
          trigger: 'blur'
        }],
        timeOfEntry: [{
          required: true,
          message: '请输入部门',
          trigger: 'change'
        }],
        correctionTime: []
      },
      workNumberOptions: [{
        'label': '选项一',
        'value': 1
      }, {
        'label': '选项二',
        'value': 2
      }]
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onNodeClick(item) {
      console.log(item)
      this.formData.departmentName = item.name
      this.showTree = false
    },
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      this.departs = tranListToTreeData(depts)
      this.loading = false
    },
    onOpen() {},
    onClose() {
      // this.$refs['addEmployeeRef'].resetFields()
      this.close()
    },
    close() {
      this.formData = this.$options.data().formData
      this.$refs.addEmployeeRef.resetFields()
      this.$emit('update:showDialog', false)
    },
    handelConfirm() {
      this.$refs['addEmployeeRef'].validate(async valid => {
        if (!valid) return
        // 发请求
        await addEmployee(this.formData)
        // 提示成功
        this.$message.success('操作成功')
        // 刷新列表
        this.$parent.getEmployeeList()
        // 关闭
        this.close()
      })
    }
  }
}

</script>
<style>
</style>
